Imports WinXound_Net.gVar


Public Class wxImportExport


    Public Sub ImportORCSCO(ByVal SciEdit As SCI_EDIT, ByRef SciEditFileName As String, _
                      ByRef WorkDir As Boolean)

        Dim tempSciEdit1 As New SCI_EDIT
        Dim tempSciEdit2 As New SCI_EDIT

        Dim OrcFileName As String = SciEditFileName.Remove(SciEditFileName.Length - 4, 4) & ".orc"
        Dim ScoFileName As String = SciEditFileName.Remove(SciEditFileName.Length - 4, 4) & ".sco"


        'Verify that files exists and load they
        If My.Computer.FileSystem.FileExists(OrcFileName) Then
            tempSciEdit1.Open(OrcFileName)
        End If
        If My.Computer.FileSystem.FileExists(ScoFileName) Then
            tempSciEdit2.Open(ScoFileName)
        End If


        'IMPORT
        With SciEdit
            .AppendText("<CsoundSynthesizer>" & vbCrLf & vbCrLf)
            .AppendText("<CsOptions>" & vbCrLf & "</CsOptions>" & vbCrLf & vbCrLf)
            .AppendText("<CsInstruments>" & vbCrLf & vbCrLf)
            .AppendText(tempSciEdit1.GetText & vbCrLf & vbCrLf)
            .AppendText("</CsInstruments>" & vbCrLf & vbCrLf)
            .AppendText("<CsScore>" & vbCrLf & vbCrLf)
            .AppendText(tempSciEdit2.GetText & vbCrLf & vbCrLf)
            .AppendText("</CsScore>" & vbCrLf & vbCrLf)
            .AppendText("</CsoundSynthesizer>" & vbCrLf & vbCrLf)
        End With

        SciEditFileName = SciEditFileName.Remove(SciEditFileName.Length - 4, 4) & ".csd"

        tempSciEdit1.Dispose()
        tempSciEdit2.Dispose()

        SciEdit.SetFocus()

    End Sub


    Public Sub ExportOrcSco(ByVal SciEdit As SCI_EDIT, ByVal SciEditFileName As String, _
                      ByRef WorkDir As Boolean)

        Dim InizioOrchestra As Long
        Dim FineOrchestra As Long
        Dim InizioScore As Long
        Dim FineScore As Long


        Dim RichTextBox1 As New RichTextBox
        Dim RichTextBox2 As New RichTextBox
        Dim RichTextBox3 As New RichTextBox

        RichTextBox1.Text = SciEdit.GetText
        RichTextBox2.Text = ""
        RichTextBox3.Text = ""

        'Find <CsInstruments> tags
        Try
            InizioOrchestra = RichTextBox1.Find("<CsInstruments>", 0, Len(RichTextBox1.Text), 2 Or 4)
            FineOrchestra = RichTextBox1.Find("</CsInstruments>", 0, Len(RichTextBox1.Text), 2 Or 4)
        Catch
            MsgBox("Conversion Error!", vbCritical, "Error!")
            RichTextBox1.Dispose()
            RichTextBox2.Dispose()
            RichTextBox3.Dispose()
            Exit Sub
        End Try

        RichTextBox1.SelectionStart = InizioOrchestra + Len("<CsInstruments>")
        RichTextBox1.SelectionLength = FineOrchestra - RichTextBox1.SelectionStart

        'Set Text in RTB2
        RichTextBox2.AppendText(RichTextBox1.SelectedText)

        'Deselect text in richtextbox
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionStart = 0


        'Find <CsScore> tags
        Try
            InizioScore = RichTextBox1.Find("<CsScore>", 0, Len(RichTextBox1.Text), 2 Or 4)
            FineScore = RichTextBox1.Find("</CsScore>", 0, Len(RichTextBox1.Text), 2 Or 4)

            RichTextBox1.SelectionStart = InizioScore + Len("<CsScore>")
            RichTextBox1.SelectionLength = FineScore - RichTextBox1.SelectionStart
        Catch
            MsgBox("Conversion Error!", vbCritical, "Error!")
            RichTextBox1.Dispose()
            RichTextBox2.Dispose()
            RichTextBox3.Dispose()
            Exit Sub
        End Try

        'Set Text in RTB3
        RichTextBox3.AppendText(RichTextBox1.SelectedText)



        'Save Files as ORC and SCO
        Dim saveFileDialog1 As New SaveFileDialog

        If SciEditFileName <> "[Untitled]" Then
            saveFileDialog1.FileName = SciEditFileName.Remove(SciEditFileName.Length - 4, 4)
        End If
        'saveFileDialog1.Filter = "CSound Files (*.orc;*.sco)|*.orc;*.sco"
        saveFileDialog1.FilterIndex = 1
        saveFileDialog1.RestoreDirectory = True

        If WorkDir Then
            saveFileDialog1.InitialDirectory = Preferences.Directory.WorkingDir
            WorkDir = False
        End If


        ' Determine if the user selected a file name from the saveFileDialog.
        If (saveFileDialog1.ShowDialog() = DialogResult.OK) _
            And (saveFileDialog1.FileName.Length) > 0 Then

            RichTextBox2.SaveFile(saveFileDialog1.FileName & ".orc", RichTextBoxStreamType.PlainText)
            RichTextBox3.SaveFile(saveFileDialog1.FileName & ".sco", RichTextBoxStreamType.PlainText)

            MsgBox("Export done", MsgBoxStyle.Information, "Export file ...")
        End If

        RichTextBox1.Dispose()
        RichTextBox2.Dispose()
        RichTextBox3.Dispose()


        SciEdit.SetFocus()

    End Sub


    Public Sub ImportORC(ByVal SciEdit As SCI_EDIT, ByRef OrcFileName As String)

        Dim tempSciEdit1 As New SCI_EDIT

        tempSciEdit1.Open(OrcFileName)
        tempSciEdit1.InsertText(0, vbCrLf & vbCrLf)
        tempSciEdit1.AppendText(vbCrLf)


        Dim StringToFind As String
        Dim mEnd As Int32 = SciEdit.GetTextLength - 1
        Dim mFindPos As Int32 = -1
        Dim mFindPosStart As Int32
        Dim mFindPosEnd As Int32

        'Find <CsInstruments>
        StringToFind = "<CsInstruments>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosStart = mFindPos + 15
        Else
            tempSciEdit1.InsertText(0, "<CsInstruments>" & vbCrLf)
        End If

        'Find </CsInstruments>
        mFindPos = -1
        StringToFind = "</CsInstruments>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosEnd = mFindPos
        Else
            tempSciEdit1.AppendText(vbCrLf & "</CsInstruments>")
        End If

        SciEdit.SetSelectionStart(mFindPosStart)
        SciEdit.SetSelectionEnd(mFindPosEnd)

        SciEdit.ReplaceSel(tempSciEdit1.GetText)

        tempSciEdit1.Dispose()
        SciEdit.SetFocus()

    End Sub

    Public Sub ImportSCO(ByVal SciEdit As SCI_EDIT, ByRef SCOFileName As String)

        Dim tempSciEdit1 As New SCI_EDIT

        tempSciEdit1.Open(SCOFileName)
        tempSciEdit1.InsertText(0, vbCrLf & vbCrLf)
        tempSciEdit1.AppendText(vbCrLf)


        SciEdit.ShowLines(0, SciEdit.GetLineCount - 1)

        Dim StringToFind As String
        Dim mEnd As Int32 = SciEdit.GetTextLength - 1
        Dim mFindPos As Int32 = -1
        Dim mFindPosStart As Int32
        Dim mFindPosEnd As Int32

        'Find <CsScore>
        StringToFind = "<CsScore>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosStart = mFindPos + 9
        Else
            tempSciEdit1.InsertText(0, "<CsScore>" & vbCrLf)
        End If

        'Find </CsScore>
        mFindPos = -1
        StringToFind = "</CsScore>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosEnd = mFindPos
        Else
            tempSciEdit1.AppendText(vbCrLf & "</CsScore>")
        End If

        SciEdit.SetSelectionStart(mFindPosStart)
        SciEdit.SetSelectionEnd(mFindPosEnd)

        SciEdit.ReplaceSel(tempSciEdit1.GetText)

        tempSciEdit1.Dispose()
        SciEdit.SetFocus()

    End Sub




    Public Sub ExportORC(ByVal SciEdit As SCI_EDIT, ByRef OrcFileName As String)

        Dim RichTextBox1 As New RichTextBox

        RichTextBox1.Text = ""

        SciEdit.ShowLines(0, SciEdit.GetLineCount - 1)

        Dim StringToFind As String
        Dim mEnd As Int32 = SciEdit.GetTextLength - 1
        Dim mFindPos As Int32 = -1
        Dim mFindPosStart As Int32
        Dim mFindPosEnd As Int32

        'Find <CsInstruments>
        StringToFind = "<CsInstruments>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosStart = mFindPos + 15
        Else
            MessageBox.Show("Unable to find <CsInstruments> section", "Error", _
                            MessageBoxButtons.OK)
            Exit Sub
        End If

        'Find </CsInstruments>
        mFindPos = -1
        StringToFind = "</CsInstruments>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosEnd = mFindPos
        Else
            MessageBox.Show("Unable to find </CsInstruments> section", "Error", _
                            MessageBoxButtons.OK)
            Exit Sub
        End If

        SciEdit.SetSelectionStart(mFindPosStart)
        SciEdit.SetSelectionEnd(mFindPosEnd)

        RichTextBox1.AppendText(SciEdit.GetSelText)
        RichTextBox1.SaveFile(OrcFileName, RichTextBoxStreamType.PlainText)

        RichTextBox1.Dispose()
        SciEdit.SetSelectionStart(mFindPosStart - 15)
        SciEdit.SetSelectionEnd(mFindPosStart - 15)
        SciEdit.SetFocus()

    End Sub

    Public Sub ExportSCO(ByVal SciEdit As SCI_EDIT, ByRef ScoFileName As String)

        Dim RichTextBox1 As New RichTextBox

        RichTextBox1.Text = ""

        SciEdit.ShowLines(0, SciEdit.GetLineCount - 1)

        Dim StringToFind As String
        Dim mEnd As Int32 = SciEdit.GetTextLength - 1
        Dim mFindPos As Int32 = -1
        Dim mFindPosStart As Int32
        Dim mFindPosEnd As Int32

        'Find <CsScore>
        StringToFind = "<CsScore>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosStart = mFindPos + 9
        Else
            MessageBox.Show("Unable to find <CsScore> section", "Error", _
                            MessageBoxButtons.OK)
            Exit Sub
        End If

        'Find </CsScore>
        mFindPos = -1
        StringToFind = "</CsScore>"
        SciEdit.SetTargetStart(0)
        SciEdit.SetTargetEnd(mEnd)
        SciEdit.SetSearchFlags(SCFIND_REGEXP Or SCFIND_MATCHCASE)
        mFindPos = SciEdit.SearchInTarget(StringToFind)
        If mFindPos > -1 Then
            mFindPosEnd = mFindPos
        Else
            MessageBox.Show("Unable to find </CsScore> section", "Error", _
                            MessageBoxButtons.OK)
            Exit Sub
        End If

        SciEdit.SetSelectionStart(mFindPosStart)
        SciEdit.SetSelectionEnd(mFindPosEnd)

        RichTextBox1.AppendText(SciEdit.GetSelText)
        RichTextBox1.SaveFile(ScoFileName, RichTextBoxStreamType.PlainText)

        RichTextBox1.Dispose()
        SciEdit.SetSelectionStart(mFindPosStart - 9)
        SciEdit.SetSelectionEnd(mFindPosStart - 9)
        SciEdit.SetFocus()

    End Sub



End Class
